<?php

namespace Frieza\Modules\Image\Controllers;

use App\Http\Controllers\Controller;
use App\Pockets\GridDataModel as pGridDataModel;
use Doraemon\model\Live\LiveRoom as mLiveRoom;
use Doraemon\tools\Videos;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Form;
use Encore\Admin\Widgets\Tab;
use Illuminate\Http\Request;
use Hhzbeads\Img\ImgUpload;
use Doraemon\tools\ImgUrl;
use Illuminate\Support\MessageBag;

class UploadController extends Controller
{
    public function add(Request $request, Content $content, $id = 0)
    {
        //直播栏目列表
        $form = new Form([]);
        $form->multipleImage('img', '图片');
        $form->action(url('admin/image/upload'));
        $tab = new Tab();
        $tab->add('上传图片', $form, true);
        $tab->addLink('上传视频', url('admin/image/addVideo'));
        $content->row($tab);
        return $content->header('上传图片');
    }

    public function upload(Content $content)
    {
        $files = $_FILES;
        $list = [];
        if ($files['img']['tmp_name']) {
            foreach ($files['img']['tmp_name'] as $key => $tmpName) {
                $res = ImgUpload::upload(ImgUrl::APPTYPE_OP, $tmpName);
                $url = ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $res['ori_pic_id'], ImgUrl::IMAGE_SIZE_OP_ORI);
                $list[] = [
                    'url' => $url,
                    'text' => $url,
                ];
            }
        }
        $gridModel = new pGridDataModel($list);
        $gridList = Admin::grid($gridModel,
            function (Grid $grid) use ($gridModel) {
                $grid->disableExport();
                $grid->disableFilter();
                $grid->disableCreateButton();
                $grid->disableRowSelector();
                $grid->disableActions();
                $grid->column('url', '图片')->image($this->url, 100, 100);
                $grid->column('text', '复制图片地址')->copyable();
            }
        );
        return $content
            ->breadcrumb(['text' => '上传图片', 'url' => '/image/add'], ['text' => '上传结果', 'url' => ''])
            ->body($gridList);
    }

    public function addVideo(Request $request, Content $content)
    {
        $form = new Form([]);
        $form->videoUpload('video_id', '视频')->help('单个视频上传');
        $form->action(url('admin/image/uploadVideo'));
        $tab = new Tab();
        $tab->addLink('上传图片', url('admin/image/add'));
        $tab->add('上传视频', $form, true);
        $content->row($tab);
        return $content->header('上传视频');
    }

    public function uploadVideo(Request $request)
    {
        $videoId = $request->input('video_id', '');
        if ($videoId) {
            return redirect()->away(url('admin/image/getVideo/' . $videoId), 301);
        } else {
            $error = new MessageBag([
                'title' => '操作失败,未提供videoId'
            ]);
            return back()->withInput()->with(compact('error'));
        }
    }

    public function getVideo(Request $request, Content $content, $videoId = '')
    {
        $list = [];
        $videoInfo = $videoId ? \Doraemon\model\Videos::get('*', ['id' => $videoId]) : [];
        if ($videoInfo) {
            $coverPicUrl = \Doraemon\tools\ImgUrl::getUrl(ImgUrl::APPTYPE_APP, $videoInfo['cover_pic_id'], ImgUrl::IMAGE_SIZE_ORI_NPHONE);
            $playDomain = Videos::getPlayDomain($videoInfo["area"]);
            $showPlayUrl = $playDomain . $videoInfo['ori_play_url'];
            $urlArr = parse_url($showPlayUrl);
            $playUrl = '//' . $urlArr['host'] . $urlArr['path'];
            $list[] = [
                'video_id' => $videoId,
                'cover_pic_url' => $coverPicUrl,
                'video_play_url' => $playUrl,
                'show_play_url' => $showPlayUrl
            ];
        }
        $gridModel = new pGridDataModel($list);
        $gridList = Admin::grid($gridModel,
            function (Grid $grid) use ($gridModel) {
                $grid->disableExport();
                $grid->disableFilter();
                $grid->disableCreateButton();
                $grid->disableRowSelector();
                $grid->disableActions();
                $grid->column('video_id', '视频Id');
                $grid->column('cover_pic_url', '视频封面图')->image();
                $grid->column('video_play_url', '视频播放预览')->display(function () {
                    return '<iframe src="' . $this->video_play_url . '" width="500" height="290"></iframe>';
                });
                $grid->column('show_play_url', '复制视频播放地址')->copyable();
            }
        );
        $html = '<span class="help-block" style="color: red"><i class="fa fa-info-circle"></i>&nbsp;如果还未展示视频和链接，请尝试刷新页面</span>';
        return $content
            ->breadcrumb(['text' => '上传视频', 'url' => '/image/addVideo'], ['text' => '上传结果', 'url' => ''])
            ->row($html . $gridList->render() . $html);
    }
}